package com.liu.webstore.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @Author LiuYumei
 * @Date 2022/8/2 10:19 AM
 */
@Configuration
@EnableRedisRepositories
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置连接redis工厂
        template.setConnectionFactory(factory);
        // 设置key的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // 设置value的序列化方式
        template.setValueSerializer(RedisSerializer.json());
        // 设置Hash的key的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // 设置Hash的value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());
        // 初始化RedisTemplate的一些参数设置
        template.afterPropertiesSet();
        return template;
    }
}
